package com.neu.fenli2023623.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.fenli2023623.dto.RoleDto;

import com.neu.fenli2023623.entity.Role;

import com.neu.fenli2023623.service.RoleService;
import com.neu.fenli2023623.util.ObjectExcelRead;
import com.neu.fenli2023623.util.ObjectExcelView;
import com.neu.fenli2023623.util.PageData;
import com.neu.fenli2023623.util.ResultVo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @ApiOperation(value = "查询角色")
    @ApiResponses({
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权访问"),
            @ApiResponse(code = 404,message = "未找到")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "查询的角色名", required = false, dataType = "RoleDto", paramType = "body")
    })
    @PostMapping("queryAll")
    public ResultVo queryAll(@RequestBody RoleDto role){
        PageHelper.startPage(role.getCurrentPage(),role.getPageSize(),true);
        List<Role> roles=roleService.selectAll(role);
        PageInfo<Role> pageInfo=new PageInfo<>(roles);
        return ResultVo.ok().data("List",roles).data("total",pageInfo.getTotal());
    }
    @ApiOperation(value = "添加角色")
    @ApiResponses({
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权访问"),
            @ApiResponse(code = 404,message = "未找到")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色信息", required = true, dataType = "Role", paramType = "body")
    })
    @PostMapping("")
    public ResultVo insert(@RequestBody Role role){
        int i=roleService.insertSelective(role);
        return i>0?ResultVo.ok("添加成功"):ResultVo.error("添加失败");
    }
    @ApiOperation(value = "删除角色")
    @ApiResponses({
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权访问"),
            @ApiResponse(code = 404,message = "未找到")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long", paramType = "path")
    })
    @DeleteMapping("")
    public ResultVo del(@PathVariable Long id){
        int i=roleService.del(id);
        return i>0?ResultVo.ok("删除成功"):ResultVo.error("删除失败");
    }
    @ApiOperation(value = "更新角色")
    @ApiResponses({
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权访问"),
            @ApiResponse(code = 404,message = "未找到")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色信息", required = true, dataType = "Role", paramType = "body")
    })
    @PutMapping("")
    public ResultVo update(@RequestBody Role role){
        int i=roleService.update(role);
        return i>0?ResultVo.ok("更新成功"):ResultVo.error("更新失败");
    }

    @ApiOperation(value = "导出Excel")
    @PostMapping(value = "/exportExcel")
    public ModelAndView exportExcel(@RequestBody Role role) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("角色名"); // 1
            titles.add("角色描述"); // 2
            titles.add("角色默认挂号费"); // 3
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<Role> varOList = roleService.selectExportData(role);
            List<PageData> varList = new ArrayList<>();
            for (Role roles : varOList) {
                PageData vpd = new PageData();
                vpd.put("var1",roles.getName());
                vpd.put("var2",roles.getDescription());
                vpd.put("var3",roles.getDefaultRegistrationFee());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    @ApiOperation(value = "导入Excel")
    @PostMapping(value="/importExcel")
    public ResultVo importExcel(MultipartFile file, HttpServletRequest request) throws Exception{

        if (null != file && !file.isEmpty()) {
            //执行读EXCEL操作,读出的数据导入List 2:从第2行开始；0:从第A列开始；0:第0个sheet
            List<PageData> listPd = (List) ObjectExcelRead.readExcel(file.getInputStream(), 1, 0, 0);
            for (PageData pageData : listPd) {
                Role role = new Role();

                role.setName(pageData.getString("var0"));
                role.setDescription(pageData.getString("var1"));
                role.setDefaultRegistrationFee(Integer.parseInt(pageData.getString("var2")));
                roleService.insertSelective(role);
            }
        }
        return ResultVo.ok("导入成功");
    }
}
